//
//  ResultCVCell.m
//  KeepFit
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 Fuzzie Liu. All rights reserved.
//

#import "ResultCVCell.h"
#import "ResultTableViewCell.h"
#import "FoodDetail.h"
@implementation ResultCVCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView registerClass:[ResultTableViewCell class] forCellReuseIdentifier:@"resultCell"];
        
        self.commonResultArr = [NSMutableArray array];
        self.caloryResultArr = [NSMutableArray array];
        self.proteinResultArr = [NSMutableArray array];
        self.fatResultArr = [NSMutableArray array];
        
        
        [self.contentView addSubview: self.tableView];
        self.tableView.footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(freshData)];
       
    }
    return self;
}

- (void)freshData
{
    if (self.current_page == 10)
    {
        self.tableView.footer.state = MJRefreshStateNoMoreData;
        self.current_page = [self.total_pages integerValue];
    }
    else
    {
        self.current_page += 1;
        [self loadData];
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    }
 
    
}
- (void)loadData
{
    NSString *pageStr = [NSString stringWithFormat:@"&page=%ld",self.current_page+1];
    self.urlStr = [self.urlStr stringByAppendingString: pageStr];
    [NetWorkingCache getUrl:self.urlStr success:^(id result) {
        self.foodResultArr = [NSMutableArray array];
        NSArray *resultArr = [result objectForKey:@"foods"];
        for (NSDictionary *resultDic in resultArr)
        {
            FoodDetail *foodDetail = [FoodDetail modelWithDic:resultDic];
            
           
            switch (self.groupIndex)
            {
                case 1:
                    [self.commonResultArr addObject:foodDetail];
                    break;
                case 2:
                    [self.caloryResultArr addObject:foodDetail];
                    break;
                case 3:
                    [self.proteinResultArr addObject:foodDetail];
                    break;
                case 4:
                    [self.fatResultArr addObject:foodDetail];
                    break;
                default:
                    break;
            }
            
            
        }
        switch (self.groupIndex)
        {
            case 1:
                self.foodResultArr = self.commonResultArr;
                break;
            case 2:
                self.foodResultArr = self.caloryResultArr;
                break;
            case 3:
                self.foodResultArr = self.proteinResultArr;
                break;
            case 4:
                self.foodResultArr = self.fatResultArr;
                break;
            default:
                break;
        }
        [self.tableView reloadData];

        
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.foodResultArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"resultCell"];
   
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        FoodDetail *foodDetail = [self.foodResultArr objectAtIndex:indexPath.row];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:foodDetail.thumb_image_url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
        
        
        cell.nameLabel.text = foodDetail.name;
        cell.nameLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
        NSString *unitStr = [NSString string];
        
        
        switch ([foodDetail.health_light intValue])
        {
            case 1:
                cell.typeImageView.image = [UIImage imageNamed:@"qing.png"];
                break;
            case 2:
                cell.typeImageView.image = [UIImage imageNamed:@"jiaozhong.png"];
                break;
            case 3:
                cell.typeImageView.image = [UIImage imageNamed:@"zhong.png"];
                break;
                
            default:
                break;
        }
        
        switch (self.groupIndex)
        {
            case 1:
                unitStr = @"大卡/100克";
                cell.energyLabel.text = [NSString stringWithFormat:@"%@%@",foodDetail.calory,unitStr];
                break;
            case 2:
                unitStr = @"大卡/100克";
                cell.energyLabel.text = [NSString stringWithFormat:@"%@%@",foodDetail.calory,unitStr];
                break;
            case 3:
                unitStr = @"克/100克";
                cell.energyLabel.text = [NSString stringWithFormat:@"%@%@",foodDetail.protein,unitStr];
                break;
            case 4:
                unitStr = @"克/100克";
                if ([foodDetail.fat isEqualToString:@""])
                {
                    foodDetail.fat = @"0.0";
                }
                cell.energyLabel.text = [NSString stringWithFormat:@"%@%@",foodDetail.fat,unitStr];
                break;
                
            default:
                break;
        }
        
        return cell;
        
}

    
    
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FoodDetail *food = [self.foodResultArr objectAtIndex:indexPath.row];
    [self.pushdelegate pushFoodVCWithStr:food.code];
    
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.tableView.frame = CGRectMake(0, 0, layoutAttributes.bounds.size.width, layoutAttributes.bounds.size.height - 60);
}




@end
